import requestInstance from "@/utils/requestInstance";
import { ResType, Article, CommentList, ArticleDetail } from "./shared";

/**
 * 获取文章列表
 * @param { page: number, pageSize: number }
 * @returns
 */
export async function getArticleList({
  page,
  pageSize = 10,
}: {
  page: number;
  pageSize?: number;
}) {
  const res = await requestInstance.get<ResType<Article[]>>(
    "/public/getArticleList",
    {
      params: { page, pageSize },
    }
  );

  return res.data;
}

/**
 * 获取文章详情
 * @param id 文章id
 * @return
 */
export async function getArticleDetail(id: string) {
  const res = await requestInstance.get<ResType<ArticleDetail>>(
    "/public/getArticleDetail",
    {
      params: { articleId: id },
    }
  );

  return res.data;
}

/**
 * 用户获取需要修改的文章
 * @param _id 文章id
 * @return
 */
export async function userGetArticleToEdit(_id: string) {
  const res = await requestInstance.get<ResType<ArticleDetail>>(
    "/private/getAuthorChangeArticle",
    {
      params: {
        articleId: _id,
      },
    }
  );

  return res.data;
}

/**
 * 用户发布文章或者是更新文章
 * @param title 文章标题
 * @param summary 文章简介
 * @param content 文章内容
 * @param createTime 文章创建时间
 * @param draftId 草稿id
 * @param articleId 文章id
 * @return
 */
export async function userPublishArticle({
  title,
  summary,
  content,
  createTime,
  draftId,
  articleId,
}: {
  title: string;
  summary: string;
  content: string;
  createTime: string;
  draftId?: string;
  articleId?: string;
}) {
  const res = await requestInstance.post<ResType<string>>(
    "/private/publishArticle",
    {
      title,
      summary,
      content,
      createTime,
      draftId,
      articleId,
    }
  );

  return res.data;
}

/**
 * 用户评论文章
 * @param articleId 文章id
 * @param createTime 评论创建时间
 * @param content 评论内容
 * @return
 */
export async function userCommentArticle({
  articleId,
  createTime,
  content,
}: {
  articleId: string;
  createTime: string;
  content: string;
}) {
  const res = await requestInstance.post<ResType<string>>(
    "/private/addComment",
    {
      articleId,
      createTime,
      content,
    }
  );

  return res.data;
}

/**
 * 删除文章
 * @param articleId 文章id
 * @return
 */
export async function deleteArticle(articleId: string) {
  const res = await requestInstance.post<ResType<string>>(
    "/private/deleteArticle",
    {
      articleId,
    }
  );

  return res.data;
}

/**
 * 文章浏览数增加
 * @param articleId 文章id
 * @return
 */
export async function addArticleViews(articleId: string) {
  const res = await requestInstance.get<ResType<string>>(
    "/public/addArticleViewCount",
    {
      params: {
        articleId,
      },
    }
  );

  return res.data;
}

/**
 * 用户添加评论
 * @param articleId 文章id
 * @param content 评论内容
 * @param createTime 评论创建时间
 * @return
 */
export async function userAddComment(
  articleId: string,
  content: string,
  createTime?: string
) {
  const res = await requestInstance.post<ResType<CommentList[]>>(
    "/private/addComment",
    {
      articleId,
      content,
      createTime: createTime || new Date().toISOString(),
    }
  );

  return res.data;
}

/**
 * 用户删除评论
 * @param articleId 文章id
 * @param commentId 评论id
 * @return
 */
export async function userDeleteComment(articleId: string, commentId: string) {
  const res = await requestInstance.delete<ResType<string>>(
    "/private/deleteComment",
    {
      params: {
        articleId,
        commentId,
      },
    }
  );

  return res.data;
}

/**
 * 用户添加评论的回复
 * @param articleId 文章id
 * @param commentId 评论id
 * @param content 回复内容
 * @param createTime 回复创建时间
 * @return
 */
export async function userAddReply(
  articleId: string,
  commentId: string,
  content: string,
  createTime: string
) {
  const res = await requestInstance.post<ResType<CommentList>>(
    "/private/addCommentReply",
    {
      articleId,
      commentId,
      content,
      createTime,
    }
  );

  return res.data;
}

/**
 * 用户删除评论的回复
 * @param articleId 文章id
 * @param commentId 评论id
 * @param replyId 回复id
 * @return
 */
export async function userDeleteReply(
  articleId: string,
  commentId: string,
  replyId: string
) {
  const res = await requestInstance.delete<ResType<string>>(
    "/private/deleteCommentReply",
    {
      params: {
        articleId,
        commentId,
        replyId,
      },
    }
  );

  return res.data;
}
